package com.cskaoyan.market.service.wx.impl;
import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketCommentExample;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.mapper.MarketCommentMapper;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.service.wx.WxCommentService;
import com.cskaoyan.market.vo.wx.lhh.CommentPostVo;
import com.cskaoyan.market.vo.wx.sun.CommentVo;
import com.cskaoyan.market.vo.wx.sun.UserInfoVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class WxCommentServiceImpl implements WxCommentService {
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketUserMapper marketUserMapper;

    // 字段的对应，ZD mapper

    /**
     * 获取评论列表
     * @param valueId
     * @param type
     * @param limit
     * @param page
     * @param showType
     * @return
     */
    @Override
    public List<CommentVo> getCommentList(Integer valueId, Byte type, Integer limit, Integer page, Integer showType) {
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(valueId).andTypeEqualTo(type).andDeletedEqualTo(false);

        // showType有图的评论列表
        if (showType == 1){
            criteria.andHasPictureEqualTo(true);
        }
//        // 查询分页评论
        PageHelper.startPage(page,limit);// c r d d x
        // 返回的多
        List<MarketComment> commentList = marketCommentMapper.selectByExample(example);
        // 进行封装
        List<CommentVo> result = new ArrayList<>();
        for (MarketComment comment : commentList) {
            CommentVo commentVo = new CommentVo();
            // 先填写userInfo
            Integer userId = comment.getUserId();
            MarketUser user = marketUserMapper.selectByPrimaryKey(userId);
            UserInfoVo userInfoVo = new UserInfoVo();
            userInfoVo.setNickName(user.getNickname());
            userInfoVo.setAvatarUrl(user.getAvatar());

            //填写外部
            commentVo.setUserInfo(userInfoVo);
            commentVo.setAddTime(comment.getAddTime());
            commentVo.setPicList(comment.getPicUrls());
            commentVo.setAdminContent(comment.getAdminContent());
            commentVo.setContent(comment.getContent());

            result.add(commentVo);
        }

        return result;
    }

    /**
     * 获取总的评论数
     * @param valueId
     * @param type
     * @return
     */

    @Override
    public HashMap<String, Integer> getCommentCount(Integer valueId, Integer type) {

        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(valueId)
                .andTypeEqualTo(Byte.valueOf(type.toString()))
                .andDeletedEqualTo(false);
        // 获取到总数
        int allCount = (int) marketCommentMapper.countByExample(example);
        // showType条件
        criteria.andHasPictureEqualTo(true);
        // showType总数
        int hasPictureCount = (int) marketCommentMapper.countByExample(example);
        // 封装对象
        HashMap<String, Integer> countMap = new HashMap<>();
        countMap.put("allCount",allCount);
        countMap.put("hasPicCount",hasPictureCount);
        return countMap;
    }

    @Override
    public CommentPostVo addPostComment(CommentPostVo postVo) {
        MarketComment comment = new MarketComment();
        comment.setValueId(postVo.getValueId());
        comment.setType(postVo.getType());
        comment.setContent(postVo.getContent());
        comment.setUserId(postVo.getUserId());
        comment.setHasPicture(postVo.getHasPicture());
        comment.setPicUrls(postVo.getPicUrls());
        comment.setStar(postVo.getStar());
        comment.setAddTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());
        comment.setDeleted(false);

        marketCommentMapper.insertSelective(comment);
        postVo.setId(comment.getId());
        postVo.setAddTime(comment.getAddTime());
        postVo.setUpdateTime(comment.getUpdateTime());

        return postVo;
    }

}
